<template>
  <div class="home">
    <tab-bar></tab-bar>
    <header class="home-header wrap" :class="{ active: headerActive }">
      <i class="iconfont icon-caidan"></i>
      <div class="header-search">
        <span class="app-name">G</span>
        <i class="iconfont icon-search"></i>
        <input
          type="text"
          class="search-title"
          placeholder="家电返场同价11.11"
        />
        <!-- <span class="search-title">家电返场同价11.11</span> -->
      </div>
      <router-link to="/login" tag="span" v-if="!isLogin">登录</router-link>
      <router-link tag="span" to="./user" v-else>
        <i class="iconfont icon-iconyonghu"></i>
      </router-link>
    </header>
    <!-- 轮播图区域 -->
    <van-swipe
      :autoplay="3000"
      indicator-color="white"
      class="swiper-container"
    >
      <van-swipe-item v-for="item in images" :key="item.categoryId">
        <img :src="item.imgUrl" class="swiper_img" alt="" />
      </van-swipe-item>
    </van-swipe>

    <!-- 分类 -->
    <section class="category-list">
      <div
        v-for="item in shoppSort"
        :key="item.name"
        @click="getProduct(item.categoryId)"
      >
        <img :src="item.imgUrl" />
        <span>{{ item.name }}</span>
      </div>
    </section>

    <!-- 首页购物楼层 -->
    <section class="floor-list">
      <div
        class="floor-wrap"
        v-for="(listData, index) in shoopFloor"
        :key="index"
      >
        <img :src="listData.headUrl" class="floor-head" />
        <div class="floor-content">
          <div
            class="floor-category"
            v-for="list in listData.list"
            :key="list.title"
          >
            <p>{{ list.title }}</p>
            <p>{{ list.desc }}</p>
            <div class="floor-products">
              <img
                v-for="(images, index) in list.products"
                :key="index"
                :src="images.imgUrl"
              />
            </div>
          </div>
        </div>
      </div>
    </section>
  </div>
</template>

<script>
import tabBar from "../../components/tabBar.vue";
//导入请求接口模块
import { sliders, category, floorlist, userInfo } from "../../api/axios";
export default {
  components: { tabBar },
  name: "home",
  data() {
    return {
      images: [],
      shoppSort: [],
      shoopFloor: [],
      headerActive: false, //判断header的红色背景
      isLogin: false,
    };
  },
  methods: {
    pageScroll() {
      // 获取滚动距离  兼容游览器写法
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop;
      scrollTop > 100
        ? (this.headerActive = true)
        : (this.headerActive = false);
    },
    //跳转购物分类列表页
    getProduct(id) {
      console.log(id);
      // this.$router.push("/product-list");
      this.$router.push({ path: "/product-list", query: { categoryId: id } });
    },
  },
  mounted() {
    // 添加滚动事件
    window.addEventListener("scroll", this.pageScroll);
  },
  beforeCreate() {
    userInfo().then((res) => {
      // console.log(res);
      res.message !== "token不存在"
        ? (this.isLogin = true)
        : (this.isLogin = false);
      if (res.status != 0) return;
      this.$store.commit("setUserInfo", res.data);
    });
  },
  created() {
    //轮播图数据请求
    sliders().then((res) => {
      this.images = res.data;
    });
    //首页购物分类数据请求
    category().then((res) => {
      this.shoppSort = res.data;
    });
    // 首页购物楼层数据请求
    floorlist().then((res) => {
      this.shoopFloor = res.data;
    });
  },
};
</script>

<style lang="scss" scoped>
@import "../../common/style/mixin";
//header上滑背景设置
.active {
  background: red;
}
.home-header {
  position: fixed;
  left: 0;
  top: 0;
  @include fj;
  width: 100%;
  height: 100px;
  line-height: 100px;
  padding: 0 30px;
  @include boxSizing;
  font-size: 30px;
  color: #fff;
  z-index: 10000;
  // background: $red;

  .icon-caidan {
    font-size: 50px;
  }

  .header-search {
    display: flex;
    width: 74%;
    height: 40px;
    line-height: 40px;
    margin: 20px 0;
    padding: 10px 0;
    color: #232326;
    background: #fff;
    @include borderRadius(40px);

    .app-name {
      padding: 0 20px;
      color: $red;
      font-size: 40px;
      font-weight: bold;
      border-right: 1px solid #666;
    }

    .icon-search {
      padding: 0 20px;
      font-size: 34px;
    }

    .search-title {
      font-size: 24px;
      color: #666;
    }
  }

  .icon-iconyonghu {
    font-size: 44px;
  }
}

// 轮播图样式
.swiper-container {
  .swiper_img {
    width: 100%;
  }
}

//分类首页购物样式
.category-list {
  display: flex;
  flex-shrink: 0;
  flex-wrap: wrap;
  width: 100%;
  padding-bottom: 26px;

  div {
    display: flex;
    flex-direction: column;
    width: 20%;
    text-align: center;

    img {
      width: 80px;
      height: 80px;
      margin: 26px auto 16px auto;
    }
  }
}

//首页购物楼层样式
.floor-list {
  width: 100%;
  padding-bottom: 100px;

  .floor-head {
    width: 100%;
    height: 80px;
    background: #f6f6f6;
  }

  .floor-content {
    display: flex;
    flex-shrink: 0;
    flex-wrap: wrap;
    width: 100%;
    @include boxSizing;

    .floor-category {
      width: 50%;
      padding: 20px;
      border-right: 1px solid #dcdcdc;
      border-bottom: 1px solid #dcdcdc;
      @include boxSizing;

      &:nth-child(2n) {
        border-right: none;
      }

      p {
        font-size: 34px;
        color: #333;

        &:nth-child(2) {
          padding: 10px 0;
          font-size: 26px;
          color: $red;
        }
      }

      .floor-products {
        display: flex;
        justify-content: space-around;
        width: 100%;

        img {
          width: 130px;
          height: 130px;
        }
      }
    }
  }
}
</style>